package com.eshop.modules.user.rest;

import com.eshop.modules.user.service.dto.UserMoneyDto;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.JSON;
import org.springframework.web.bind.annotation.DeleteMapping;
import com.eshop.modules.aop.ForbidSubmit;
import java.io.Serializable;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.validation.annotation.Validated;
import com.eshop.modules.user.domain.ShopUser;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.data.domain.Pageable;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.PostMapping;
import io.swagger.annotations.ApiOperation;
import com.eshop.logging.aop.log.Log;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestBody;
import com.eshop.modules.user.service.dto.UserQueryCriteria;
import com.eshop.modules.user.service.UserService;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import io.swagger.annotations.Api;

@Api(tags = { "商城:会员管理" })
@RestController
@RequestMapping({ "api" })
public class MemberController
{
    private final UserService userService;
    
    public MemberController(final UserService userService) {
        this.userService = userService;
    }
    
    @Log("查看下级")
    @ApiOperation("查看下级")
    @PostMapping({ "/yxUser/spread" })
    @PreAuthorize("hasAnyRole('admin','YXUSER_ALL','YXUSER_EDIT')")
    public ResponseEntity getSpread(@RequestBody final UserQueryCriteria criteria) {
        return new ResponseEntity(this.userService.querySpread(criteria.getUid(), criteria.getGrade()), HttpStatus.OK);
    }
    
    @Log("查询用户")
    @ApiOperation("查询用户")
    @GetMapping({ "/yxUser" })
    @PreAuthorize("hasAnyRole('admin','YXUSER_ALL','YXUSER_SELECT')")
    public ResponseEntity getYxUsers(final UserQueryCriteria criteria, final Pageable pageable) {
        return new ResponseEntity(this.userService.queryAll(criteria, pageable), HttpStatus.OK);
    }
    
    @Log("修改用户")
    @ApiOperation("修改用户")
    @PutMapping({ "/yxUser" })
    @PreAuthorize("hasAnyRole('admin','YXUSER_ALL','YXUSER_EDIT')")
    public ResponseEntity update(@Validated @RequestBody final ShopUser resources) {
        this.userService.saveOrUpdate(resources);
        return new ResponseEntity(HttpStatus.NO_CONTENT);
    }
    
    @ForbidSubmit
    @Log("删除用户")
    @ApiOperation("删除用户")
    @DeleteMapping({ "/yxUser/{uid}" })
    @PreAuthorize("hasAnyRole('admin','YXUSER_ALL','YXUSER_DELETE')")
    public ResponseEntity delete(@PathVariable final Integer uid) {
        this.userService.removeById((Serializable)uid);
        return new ResponseEntity(HttpStatus.OK);
    }
    
    @ForbidSubmit
    @ApiOperation("用户禁用启用")
    @PostMapping({ "/yxUser/onStatus/{id}" })
    public ResponseEntity onStatus(@PathVariable final Long id, @RequestBody final String jsonStr) {
        final JSONObject jsonObject = JSON.parseObject(jsonStr);
        final Integer status = jsonObject.getInteger("status");
        this.userService.onStatus(id, status);
        return new ResponseEntity(HttpStatus.OK);
    }
    
    @ApiOperation("修改余额")
    @PostMapping({ "/yxUser/money" })
    @PreAuthorize("hasAnyRole('admin','YXUSER_ALL','YXUSER_EDIT')")
    public ResponseEntity updatePrice(@Validated @RequestBody final UserMoneyDto param) {
        this.userService.updateMoney(param);
        return new ResponseEntity(HttpStatus.NO_CONTENT);
    }
}
